<template>
  <view class="task-end">
    <view class="task-end-name">{{ name }}</view>
    <view class="task-end-comment">
      <!-- <Comment v-model="comment" /> -->
      <Comment-Input ref="commentRef" :input-call-back="commentBack" placeholder="请输入" submit-text="" :inpit-height="350" />
    </view>
    <view class="task-end-btn">
      <button type="primary" @click="taskEndSure">确认完成</button>
    </view>
    <u-mask :show="show" />
  </view>
</template>

<script>
// 完成任务
// import Comment from '@/components/comment/Comment';
import { taskEnd } from '@/api/task';
import CommentInput from 'common/commentInput/CommentInput';
export default {
  components: {
    CommentInput
  },
  data() {
    return {
      name: '',
      show: false,
      taskEndData: {
        detailTaskGuid: '', // 子任务唯一标识（projectDetial的detailTaskGuid
        editUser: '',
        files: [],
        status: undefined,
        taskGuid: '', // 任务唯一标识guid
        content: ''
      }
    };
  },
  created() {
    this.getTaskEndData();
  },
  methods: {
    getTaskEndData() {
      const { cEnterpriseName, status, detailTaskGuid, taskGuid } = this.$route.query;
      this.name = cEnterpriseName;
      let statusEnd;
      if (Number(status) === 1 || Number(status) === 3 || Number(status) === 5) {
        statusEnd = 4;
      }
      const userId = uni.getStorageSync('userId');
      this.taskEndData = {
        ...this.taskEndData,
        detailTaskGuid,
        editUser: userId,
        status: statusEnd,
        taskGuid
      };
    },
    commentBack(value) {
      console.log(value);
      // const { content, takePhotoConfig: { fileList: files }} = value;
      const { content, files } = value;
      this.taskEndData.content = content;
      this.taskEndData.files = files;
    },
    async taskEndSure() {
      await this.$refs.commentRef.submitComment();
      const requestForm = new FormData();
      const { files } = this.taskEndData;
      const headers = files.length ? {
        'Content-Type': 'multipart/form-data'
      } : {
        'Content-Type': 'application/x-www-form-urlencoded'
      };
      files.length && (this.taskEndData['type'] = 'Y0013');
      files.length && files.forEach(file => requestForm.append('files', file.file));
      for (const key in this.taskEndData) {
        if (key !== 'files') {
          requestForm.append(key, this.taskEndData[key]);
        }
      }
      this.show = true;
      uni.showLoading({
        title: '提交中',
        mask: true
      });
      taskEnd(requestForm, headers).then(res => {
        const { success, message } = res;
        uni.hideLoading();
        setTimeout(() => {
          this.show = false;
        }, 2000);
        if (success) {
          uni.showToast({
            title: message,
            duration: 2000
          });
          setTimeout(() => {
            const { guid, programGuid, xzRole, enterpriseCode } = this.$route.query;
            this.$store.dispatch('task/getTaskDetail', {
              taskGuid: guid, programGuid, xzRole, status: 4, enterpriseCode,
              action: (res, type) => {
                if (type === '明细') {
                  const query = {
                    guid,
                    programGuid,
                    status: 4,
                    enterpriseCode,
                    xzRole
                  };
                  this.$router.replace({
                    path: '/taskdetail/taskinfo',
                    query
                  });
                }
              }
            });
          }, 2000);
        }
      });
      // const { guid, programGuid, xzRole, enterpriseCode } = this.$route.query;
      // this.$store.dispatch('task/getTaskDetail', {
      //   taskGuid: guid, programGuid, xzRole, status: 4, enterpriseCode,
      //   action: (res, type) => {
      //     if (type === '明细') {
      //       const query = {
      //         guid,
      //         programGuid,
      //         status: 4,
      //         enterpriseCode,
      //         xzRole
      //       };
      //       this.$router.push({
      //         path: '/taskdetail/taskinfo',
      //         query
      //       });
      //     }
      //   }
      // });
    }
  }
};
</script>

<style lang="scss" scoped>
.task-end {
  .task-end-name{
    height: 50px;
    background-color: #fff;
    line-height: 50px;
    padding-left: 13px;
    font-size: 17px;
    color: #303133;
    font-weight: 600;
  }
  .task-end-comment{
    padding-bottom: 70px;
  }
  .task-end-btn{
    position: fixed;
    bottom: 0;
    padding: 10px;
    width: 100%;
  }
}
</style>
